Current Location: Home> Function Categories> opendir

opendir

Open the directory handle
Name:opendir
Category:Directory functions
Programming Language:php
One-line Description:Open the directory handle.

Definition and usage

The opendir() function opens the directory handle.

Example

Open a directory, read its contents, and close:

 <?php
$dir = "/images/" ;

// Open the directory and read its contents
if ( is_dir ( $dir ) ) {
  if ( $dh = opendir ( $dir ) ) {
    while ( ( $file = readdir ( $dh ) ) !== false ) {
      echo "filename:" . $file . "<br>" ;
    }
    closedir ( $dh ) ;
  }
}
?>

result:

 filename: cat.gif
filename: dog.gif
filename: horse.gif

grammar

 opendir ( path , context ) ;
parameter describe
path Required. Specify the directory path to open.
context Optional. Specifies the environment for directory handles. context is a set of options that can modify the behavior of a directory stream.
Similar Functions
Popular Articles